﻿using SLClient;
using SLClient.Extension;
using SLClient.ServiceAPI;
using SLClient.ViewModels;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace SLClient.CustomerControls
{
    public partial class BetControlNx : UserControl
    {
        private BetType _currentBetType;
        private BetDataControl m_BetDataControl = new BetDataControl();
        private BonusControl m_BonusControl;
        private BetPlayInfo m_CurrentBetPlayInfo;
        private BetPlayTypeInfo m_CurrentBetPlayTypeInfo;
        private List<CheckBox[]> m_CurrentCheckBoxs = new List<CheckBox[]>();
        private string[] m_PosNames;
        private BetPlayTypeInfo[] m_TypeInfos;
        private const int MaxBetCount = 0x3e8;
        private const int MaxBetDataLength = 250;
        public double SelectNumberWidth = 96.0;
        private UserPlayInfo userInfo;

        public BetControlNx()
        {
            this.InitializeComponent();
            this.btBet.Click+=new RoutedEventHandler(btBet_Click);            
            this.lbSelectCount.DataContext=m_BetDataControl;
        }

        private void BetControlNx_Click(object sender, RoutedEventArgs e)
        {
            this.UpdateBetCount();
        }

        private void btAll_Click(object sender, RoutedEventArgs e)
        {
            CheckBox[] boxArray = (sender as Button).Tag as CheckBox[];
            if ((boxArray != null) && (boxArray.Length > 0))
            {
                foreach (CheckBox box in boxArray)
                {
                    box.IsChecked = true;
                }
                this.UpdateBetCount();
            }
        }

        private void btBet_Click(object sender, RoutedEventArgs e)
        {
            Func<PlayTypeInfo, bool> func = null;
            if (this.m_CurrentBetPlayInfo == null)
            {
                App.Show("玩法信息有误");
            }
            else
            {
                decimal num;
                if (!(decimal.TryParse(this.tbAmount.Text, out num) && (num >= ViewDispatcher.CurrentConfig.MinBetMoney)))
                {
                    App.Show("投注分数有误");
                }
                else
                {
                    int betCount = this.GetBetCount();
                    if (betCount < 1)
                    {
                        App.Show("没有选择投注号码");
                    }
                    else if (betCount > 0x3e8)
                    {
                        App.Show("单次投注数量超过上限(" + 0x3e8 + "注)");
                    }
                    else
                    {
                        string betNumber = this.GetBetNumber();
                        if (betNumber == "")
                        {
                            App.Show("没有选择投注号码");
                        }
                        else if (betNumber.Length > 250)
                        {
                            App.Show("投注数据长度超过上限(" + 0xfa0 + ")");
                        }
                        else
                        {
                            this.userInfo.DanBeiMoney = new decimal?(num);
                            this.userInfo.BetAmount = new int?(int.Parse(this.lbSelectCount.Text));
                            if (func == null)
                            {
                                func = new Func<PlayTypeInfo, bool>((s) => { return (s.PlayInfoId == this.m_CurrentBetPlayInfo.PlayId); });
                            }
                            this.userInfo.JiangJin = new decimal?(Enumerable.First<PlayTypeInfo>(ViewDispatcher.PlayTypeInfos, func).JiangJin);
                            this.userInfo.Fiandian = new decimal?(ViewDispatcher.LoginUser.Fandian);
                            this.userInfo.Orization = false;
                            this.userInfo.PlayTypeId = this.m_CurrentBetPlayTypeInfo.TypeId;
                            this.userInfo.PlayTypeInfoId = this.m_CurrentBetPlayInfo.PlayId;
                            this.userInfo.UserId = ViewDispatcher.LoginUser.UserId;
                            this.userInfo.ContentNumber = this.GetBetNumber();
                            this.userInfo.QIHao = this.m_BonusControl.Qihao;
                            ConfirmWindow window = new ConfirmWindow(null, this, this.userInfo, this.m_CurrentBetPlayTypeInfo.TypeName);
                            window.BetSuccess += new EventHandler(this.ConfirmWindowBetSuccess);
                            window.Show();
                        }
                    }
                }
            }
        }

        private void btBig_Click(object sender, RoutedEventArgs e)
        {
            CheckBox[] boxArray = (sender as Button).Tag as CheckBox[];
            if ((boxArray != null) && (boxArray.Length > 0))
            {
                for (int i = 0; i < boxArray.Length; i++)
                {
                    boxArray[i].IsChecked = new bool?(i >= (boxArray.Length / 2));
                }
                this.UpdateBetCount();
            }
        }

        private void btClear_Click(object sender, RoutedEventArgs e)
        {
            CheckBox[] boxArray = (sender as Button).Tag as CheckBox[];
            if ((boxArray != null) && (boxArray.Length > 0))
            {
                foreach (CheckBox box in boxArray)
                {
                    box.IsChecked = false;
                }
                this.UpdateBetCount();
            }
        }

        private void btEven_Click(object sender, RoutedEventArgs e)
        {
            CheckBox[] boxArray = (sender as Button).Tag as CheckBox[];
            if ((boxArray != null) && (boxArray.Length > 0))
            {
                for (int i = 0; i < boxArray.Length; i++)
                {
                    boxArray[i].IsChecked = new bool?(((i + this.m_CurrentBetPlayInfo.MinNumber) % 2) == 0);
                }
                this.UpdateBetCount();
            }
        }

        private void btOdd_Click(object sender, RoutedEventArgs e)
        {
            CheckBox[] boxArray = (sender as Button).Tag as CheckBox[];
            if ((boxArray != null) && (boxArray.Length > 0))
            {
                for (int i = 0; i < boxArray.Length; i++)
                {
                    boxArray[i].IsChecked = new bool?(((i + this.m_CurrentBetPlayInfo.MinNumber) % 2) == 1);
                }
                this.UpdateBetCount();
            }
        }

        private void btSmall_Click(object sender, RoutedEventArgs e)
        {
            CheckBox[] boxArray = (sender as Button).Tag as CheckBox[];
            if ((boxArray != null) && (boxArray.Length > 0))
            {
                for (int i = 0; i < boxArray.Length; i++)
                {
                    boxArray[i].IsChecked = new bool?(i < (boxArray.Length / 2));
                }
                this.UpdateBetCount();
            }
        }

        private void ChgPlayTypeInfo(BetPlayTypeInfo bpti)
        {
            if (this.m_CurrentBetPlayTypeInfo != bpti)
            {
                this.m_CurrentBetPlayTypeInfo = bpti;
                this.m_CurrentBetPlayInfo = null;
                this.betPlayTypeCheckBoxs.Children.Clear();
                foreach (BetPlayInfo info in this.m_CurrentBetPlayTypeInfo.PlayInfos)
                {
                    RadioButton button = new RadioButton();
                    button.Content=info.PlayName;
                    button.DataContext=info;
                    button.Checked+=new RoutedEventHandler(RbPlayInfoChecked);                    
                    this.betPlayTypeCheckBoxs.Children.Add(button);
                    if (info == this.m_CurrentBetPlayTypeInfo.PlayInfos[0])
                    {
                        button.IsChecked = true;
                    }
                }
            }
        }

        private void ConfirmWindowBetSuccess(object sender, EventArgs e)
        {
            this.resetSelectNumber();
            this.m_BetDataControl.ClearBetData();
            this.UpdateScaleInfo();
        }

        private int GetBetCount()
        {
            return Enumerable.Count<CheckBox>(this.m_CurrentCheckBoxs[0], 
                new Func<CheckBox,bool>((s)=>{return s.IsChecked == true;}));
        }

        private string GetBetNumber()
        {
            IEnumerable<string> enumerable = Enumerable.Select<CheckBox, string>(Enumerable.Where<CheckBox>(this.m_CurrentCheckBoxs[0], 
                           (s)=>{ return s.IsChecked == true;}),
                           (k)=>{ return k.Content.ToString();});
            return string.Join("|", enumerable);
        }

        private string GetPosName(int pos)
        {
            if (this.m_CurrentBetPlayInfo != null)
            {
                switch (this.m_CurrentBetPlayInfo.BetType)
                {
                    case 0:
                    case 3:
                        if ((this.m_CurrentBetPlayInfo.BonusOffset + pos) < this.m_PosNames.Length)
                        {
                            return this.m_PosNames[this.m_CurrentBetPlayInfo.BonusOffset + pos];
                        }
                        return "";

                    case 1:
                        return "组选";

                    case 2:
                        return "不定位";
                }
            }
            return "";
        }

        public void InitPage(string[] posnames, BetPlayTypeInfo[] typeinfos, BonusControl bonuscontrol, int lotteryCatagoryId)
        {
            this.m_PosNames = posnames;
            this.m_TypeInfos = typeinfos;
            this.m_BonusControl = bonuscontrol;
            this.m_BonusControl.BonusStatusChanged += new EventHandler(this.MBonusControlBonusStatusChanged);
            this.userInfo = new UserPlayInfo();
            this.userInfo.Orization = false;
            this.userInfo.LotteryCatagoryId = lotteryCatagoryId;
            this.betGroupPanel.Children.Clear();
            foreach (BetPlayTypeInfo info in this.m_TypeInfos)
            {
                RadioButton button = new RadioButton();
                button.Style=Application.Current.Resources["styleRadioButton_BetPlayType"] as Style;
                button.FontSize=14.0;
                button.Margin=new Thickness(1.0);
                button.Padding=new Thickness(4.0, 2.0, 4.0, 2.0);
                button.Content=info.TypeName;
                button.DataContext=info;
                button.Checked+=new RoutedEventHandler(rbtType_Checked);                
                this.betGroupPanel.Children.Add(button);
                if (info == this.m_TypeInfos[0])
                {
                    button.IsChecked = true;
                }
            }
        }

        private void MBonusControlBonusStatusChanged(object sender, EventArgs e)
        {
            this.btBet.IsEnabled = this.m_BonusControl.CurrentBonusStatus == BonusStatus.StartBet;
            this.UpdateScaleInfo();
        }

        private void RbPlayInfoChecked(object sender, RoutedEventArgs e)
        {
            RadioButton button = sender as RadioButton;
            BetPlayInfo info = button.DataContext as BetPlayInfo;
            if (this.m_CurrentBetPlayInfo != info)
            {
                this.m_BetDataControl.ClearBetData();
                this.m_CurrentBetPlayInfo = info;
                if (this.m_CurrentBetPlayInfo.IsDouble > 0)
                {
                    int num;
                    if (this.m_CurrentBetPlayInfo.BetType == 1)
                    {
                        this.SelectNumberWidth = 96.0;
                    }
                    else if (this.m_CurrentBetPlayInfo.BetNumberType == 0)
                    {
                        this.SelectNumberWidth = (this.m_CurrentBetPlayInfo.BetNumberCount * ((this.m_CurrentBetPlayInfo.MaxNumber -this.m_CurrentBetPlayInfo.MinNumber) + 1)) * ((this.m_CurrentBetPlayInfo.MaxNumber < 10) ? 9 : 0x12);
                    }
                    else
                    {
                        this.SelectNumberWidth = ((this.m_CurrentBetPlayInfo.MaxNumber - this.m_CurrentBetPlayInfo.MinNumber) + 1) * 0x12;
                    }
                    this.m_CurrentCheckBoxs.Clear();
                    this.stackSelectNumber.Children.Clear();
                    this.gridScaleInfo.Children.Clear();
                    if (Enumerable.Count<ColumnDefinition>(this.gridScaleInfo.ColumnDefinitions) != (this.m_CurrentBetPlayInfo.ShowCount + 1))
                    {
                        this.gridScaleInfo.ColumnDefinitions.Clear();
                        this.gridScaleInfo.ColumnDefinitions.Add(new ColumnDefinition());
                        for (num = 0; num < this.m_CurrentBetPlayInfo.ShowCount; num++)
                        {
                            this.gridScaleInfo.ColumnDefinitions.Add(new ColumnDefinition());
                        }
                    }
                    this.gridScaleInfo.RowDefinitions.Clear();
                    int num2 = -1;
                    this.stackSelectNumber.Visibility = Visibility.Visible;
                    this.lbSelectNumber.Visibility = Visibility.Visible;
                    string number = "";
                    int num3 = 0;
                    for (num = 0; num < this.m_CurrentBetPlayInfo.BetNumberCount; num++)
                    {
                        StackPanel panel = new StackPanel();
                        panel.Margin = new Thickness(2.0);
                        panel.Orientation = Orientation.Horizontal;
                        Border border = new Border();
                        border.Style=Application.Current.Resources["BorderStyle_BetPosName"] as Style;
                        TextBlock block = new TextBlock();
                        block.Style=Application.Current.Resources["styleTextBlock_BetPosName"] as Style;
                        block.Text=this.GetPosName(num);
                        border.Child=block;
                        panel.Children.Add(border);
                        StackPanel panel2 = new StackPanel();
                        panel2.Margin = new Thickness(5.0, 0.0, 5.0, 0.0);
                        CheckBox[] item = new CheckBox[(this.m_CurrentBetPlayInfo.MaxNumber - this.m_CurrentBetPlayInfo.MinNumber) + 1];
                        StackPanel panel3 = null;
                        for (int i = 0; i < item.Length; i++)
                        {
                            if ((i % this.m_CurrentBetPlayInfo.ShowCount) == 0)
                            {
                                panel3 = new StackPanel();
                                panel3.Margin = new Thickness(5.0, 0.0, 5.0, 0.0);
                                panel3.Orientation = Orientation.Horizontal;
                                panel2.Children.Add(panel3);
                                num2++;
                                this.gridScaleInfo.RowDefinitions.Add(new RowDefinition());
                                ShowNxInfoControl control = new ShowNxInfoControl();
                                control.Margin=new Thickness(-1.0, 0.0, 0.0, 0.0);
                                control.SetBt();
                                control.SetValue(Grid.RowProperty, num2);
                                control.SetValue(Grid.ColumnProperty, 0);
                                this.gridScaleInfo.Children.Add(control);
                            }
                            num3 = this.m_CurrentBetPlayInfo.PlayId + i;
                            number = (this.m_CurrentBetPlayInfo.MinNumber + i).ToString(this.m_CurrentBetPlayInfo.StringFormat);
                            item[i] = new CheckBox();
                            item[i].Content = number;
                            item[i].DataContext = num3;
                            item[i].Click+=new RoutedEventHandler(BetControlNx_Click);
                            panel3.Children.Add(item[i]);
                            ShowNxInfoControl control2 = new ShowNxInfoControl();
                            control2.Margin = new Thickness(-1.0, 0.0, 0.0, 0.0);
                            control2.Init(this.m_CurrentBetPlayInfo.PlayId, number, "封单", item[i]);
                            control2.SetValue(Grid.RowProperty, num2);
                            control2.SetValue(Grid.ColumnProperty, (i % this.m_CurrentBetPlayInfo.ShowCount) + 1);
                            control2.DataContext = num3;
                            control2.Clicked += new EventHandler(this.snic_Clicked);
                            this.gridScaleInfo.Children.Add(control2);
                        }
                        this.m_CurrentCheckBoxs.Add(item);
                        panel.Children.Add(panel2);
                        StackPanel panel4 = new StackPanel();
                        panel4.Orientation = Orientation.Horizontal;
                        Button button2 = new Button();
                        button2.HorizontalAlignment = HorizontalAlignment.Center;
                        button2.VerticalAlignment = VerticalAlignment.Center;
                        button2.Content = "全";
                        button2.Tag = item;
                        button2.Click+=new RoutedEventHandler(btAll_Click);
                        panel4.Children.Add(button2);
                        Button button3 = new Button();
                        button3.HorizontalAlignment = HorizontalAlignment.Center;
                        button3.VerticalAlignment = VerticalAlignment.Center;
                        button3.Content = "大";
                        button3.Tag = item;
                        button3.Click+=new RoutedEventHandler(btBig_Click);
                        panel4.Children.Add(button3);
                        Button button4 = new Button();
                        button4.HorizontalAlignment = HorizontalAlignment.Center;
                        button4.VerticalAlignment = VerticalAlignment.Center;
                        button4.Content = "小";
                        button4.Tag = item;
                        button4.Click+=new RoutedEventHandler(btSmall_Click);
                        panel4.Children.Add(button4);
                        Button button5 = new Button();
                        button5.HorizontalAlignment = HorizontalAlignment.Center;
                        button5.VerticalAlignment = VerticalAlignment.Center;
                        button5.Content = "单";
                        button5.Tag = item;
                        button5.Click+=new RoutedEventHandler(btOdd_Click);
                        panel4.Children.Add(button5);
                        Button button6 = new Button();
                        button6.HorizontalAlignment = HorizontalAlignment.Center;
                        button6.VerticalAlignment = VerticalAlignment.Center;
                        button6.Content = "双";
                        button6.Tag = item;
                        button6.Click+=new RoutedEventHandler(btEven_Click);
                        panel4.Children.Add(button6);
                        Button button7 = new Button();
                        button7.HorizontalAlignment = HorizontalAlignment.Center;
                        button7.VerticalAlignment = VerticalAlignment.Center;
                        button7.Content="清";
                        button7.Tag = item;
                        button7.Click+=new RoutedEventHandler(btClear_Click);
                        panel4.Children.Add(button7);
                        panel.Children.Add(panel4);
                        this.stackSelectNumber.Children.Add(panel);
                    }
                }
                this.UpdateScaleInfo();
                this.UpdateBetCount();
            }
        }

        private void rbtType_Checked(object sender, RoutedEventArgs e)
        {
            this.ChgPlayTypeInfo((sender as RadioButton).DataContext as BetPlayTypeInfo);
        }

        private void resetSelectNumber()
        {
            if (this.m_CurrentBetPlayInfo != null)
            {
                if (this.m_CurrentBetPlayInfo.IsDouble > 0)
                {
                    foreach (CheckBox[] boxArray in this.m_CurrentCheckBoxs)
                    {
                        foreach (CheckBox box in boxArray)
                        {
                            box.IsChecked = false;
                        }
                    }
                }
                this.UpdateBetCount();
            }
        }

        private void snic_Clicked(object sender, EventArgs e)
        {
            CheckBox bindCheckBox = (sender as ShowNxInfoControl).BindCheckBox;
            bool? nullable = bindCheckBox.IsChecked;
            bindCheckBox.IsChecked = nullable.HasValue ? new bool?(!nullable.GetValueOrDefault()) : null;
            this.BetControlNx_Click(bindCheckBox, null);
        }

        private void UpdateBetCount()
        {
            this.lbSelectCount.Text = this.GetBetCount().ToString();
        }

        public void UpdateScaleInfo()
        {
            List<ViwUserPlayInfo> list = Enumerable.ToList<ViwUserPlayInfo>(Enumerable.Where<ViwUserPlayInfo>(MainpageViewModel.NewPlayInfos, new

Func<ViwUserPlayInfo, bool>((s) => { return ((s.PlayTypeInfoId == this.m_CurrentBetPlayInfo.PlayId) && (s.QiHao == this.m_BonusControl.Qihao)); })));
            Dictionary<int, decimal> dictionary = new Dictionary<int, decimal>();
            foreach (ViwUserPlayInfo info in list)
            {
                string str;
                decimal? nullable3;
                if (this.m_CurrentBetPlayTypeInfo.TypeId == 0x10)
                {
                    str = info.PlayName.Substring(1);
                }
                else
                {
                    int startIndex = info.PlayName.IndexOf("(") + 1;
                    int length = info.PlayName.IndexOf(")") - startIndex;
                    str = info.PlayName.Substring(startIndex, length);
                }
                int key = Convert.ToInt32(str);
                if (!dictionary.ContainsKey(key))
                {
                    nullable3 = info.DanBeiMoney * info.BetAmount;
                    dictionary[key] = nullable3.Value;
                }
                else if (dictionary.ContainsKey(key))
                {
                    nullable3 = info.DanBeiMoney * info.BetAmount;
                    dictionary[key] += nullable3.Value;
                }
            }
            int num4 = 1;
            foreach (ShowNxInfoControl control in this.gridScaleInfo.Children)
            {
                int num5;
                num4++;
                if ((control.DataContext != null) && int.TryParse(control.DataContext.ToString(), out num5))
                {
                    decimal num6;
                    if (this.m_BonusControl.CurrentBonusStatus != BonusStatus.StartBet)
                    {
                        control.SetFd("封单");
                        continue;
                    }
                    decimal[] playScaleShowInfo = NxScaleInfo.GetPlayScaleShowInfo(control.PlayTypeInfoId, num5);
                    if (playScaleShowInfo == null)
                    {
                        continue;
                    }
                    int shownumber = control.Shownumber;
                    if (dictionary.TryGetValue(shownumber, out num6))
                    {
                        playScaleShowInfo[1] -= num6;
                    }
                    control.SetScale(playScaleShowInfo[0], playScaleShowInfo[1]);
                }
            }
        }

        public BetType CurrentBetType
        {
            get
            {
                return this._currentBetType;
            }
            set
            {
                this._currentBetType = value;
                if (this._currentBetType == BetType.BetSuccess)
                {
                    this.ConfirmWindowBetSuccess(this, null);
                }
            }
        }
    }
}
